package com.iplant.wms.controller.wms;

import com.iplant.wms.service.WMSService;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.wms.WMSStockFreeze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/WMSStockFreeze")
public class WMSStockFreezeController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(WMSStockFreezeController.class);

	@Autowired
	WMSService wWMSService;

	@GetMapping("/All")
	public Object All(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			/**
			 * 批号
			 */
			String wName = StringUtils.parseString(request.getParameter("Name"));
			String wProjectNo = StringUtils.parseString(request.getParameter("ProjectNo"));
			int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"));
			int wStoreRegionID = StringUtils.parseInt(request.getParameter("StoreRegionID"));
			int wStoreID = StringUtils.parseInt(request.getParameter("StoreID"));
			int wStoreAreaID = StringUtils.parseInt(request.getParameter("StoreAreaID"));
			int wStoreShelfID = StringUtils.parseInt(request.getParameter("StoreShelfID"));
			int wStoreStationID = StringUtils.parseInt(request.getParameter("StoreStationID"));
			int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"));
			String wMaterialCode = StringUtils.parseString(request.getParameter("MaterialCode"));
			String wMaterialName = StringUtils.parseString(request.getParameter("MaterialName"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
			Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<WMSStockFreeze>> wServiceResult = wWMSService.WMS_QueryStockFreezeList(wLoginUser,
					wFactoryID, wStoreRegionID, wStoreID, wStoreAreaID, wStoreShelfID, wStoreStationID, wMaterialID,
					wMaterialCode, wMaterialName, wProjectNo, wName, wActive, wStartTime, wEndTime, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Update")
	public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			WMSStockFreeze wWMSStockFreeze = CloneTool.Clone(wParam.get("data"), WMSStockFreeze.class);
			wWMSStockFreeze.EditorName = wUserName;
			wWMSStockFreeze.EditorID = wUserID;
			wWMSStockFreeze.EditTime = Calendar.getInstance();
			if (wWMSStockFreeze.ID <= 0) {

				wWMSStockFreeze.CreatorName = wUserName;
				wWMSStockFreeze.CreatorID = wUserID;
				wWMSStockFreeze.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wWMSService.WMS_UpdateStockFreeze(wBMSEmployee, wWMSStockFreeze);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wWMSStockFreeze);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Active")
	public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<WMSStockFreeze> wWMSStockFreezeList = CloneTool.CloneArray(wParam.get("data"), WMSStockFreeze.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (WMSStockFreeze wWMSStockFreeze : wWMSStockFreezeList) {

				wServiceResult = wWMSService.WMS_ActiveStockFreeze(wBMSEmployee, wWMSStockFreeze, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Delete")
	public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			WMSStockFreeze wWMSStockFreeze = CloneTool.Clone(wParam.get("data"), WMSStockFreeze.class);

			ServiceResult<Integer> wServiceResult = wWMSService.WMS_DeleteStockFreeze(wBMSEmployee, wWMSStockFreeze);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}
}
